[PHP] gethostbyname – native DNS resolution in Node.js builds #2988
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Before this PR, the
gethostbyname()PHP function always returns either an internal IP address on success, or an unchanged hostname on failure. That's because Emscripten's default DNS implementation doesn't perform real lookups.This PR enables actual DNS resolution for PHP-WASM running in Node.js via Node's
dnsmodule. When PHP code calls
gethostbyname('wordpress.org'), it now resolves and returnsthe real IP address.
Implementation details
The implementation works similarly to
FileLockManager:phpwasm-emscripten-library-dns-lookup-for-node.jsdefines a new build-time JavaScript library linked with PHP.wasm. It overrides the__emscripten_lookup_nameshipped by Emscripten with a custom callback that callsPHPLoader.syscalls.gethostbyname()to resolve the IP.PHPLoader.syscallsis an object passed toloadNodeRuntime()viaoptions.emscriptenOptions. It's an object that, for now, defines a single method:gethostbyname().Even though you may pass an actual object instance, we create the instance in the parent worker and call
gethostbyname()using a Comlink proxy. This is becauserequire('dns').lookup()is asynchronous in Node. JSPI builds can easilyawaitthe returned value, but Asyncify builds can't. Not without spending a dozen hours tweaking theASYNCIFY_ONLYlist in the PHP compile Dockerfile. Therefore, we use a synchronous Comlink proxy to turn an asynchronousgethostbyname()call into a synchronous one in Asyncify builds.Test plan
CI. See the tests shipped with this PR, confirm they pass and make sense.
cc @bcotrim @fredrikekelund